package com.tecode.controller.songSheet;

import com.tecode.bean.MusicSongsheet;
import com.tecode.bean.Songsheet;
import com.tecode.service.songsheet.SongSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/songsheet")
public class SongSheetController {
    @Autowired
    private SongSheetService sheetService;
    @GetMapping(value = "/getList")
    public String getSongSheetList(Map<String, Object> map){
        map.put("songSheets",sheetService.songsheetOrderByCreatetime());
        map.put("count",sheetService.songSheetCount());

        return "picture-list.html";
    }

    @RequestMapping(value = "/getMusic/{id}")
    public String getMusic(@PathVariable(value = "id") Integer id,Map<String, Object> map){

        map.put("musics",sheetService.selectMusicBySongSheetId(id));
        map.put("count",sheetService.musicCountBySongSheetId(id));
        map.put("songSheet",sheetService.selectById(id));
        return "picture-show.html";
    }

    @GetMapping("getSongSheetTypes")
    @ResponseBody
    public List getSongSheetTypes(@RequestParam Integer userId,@RequestParam Integer page){
        List songSheetTypes = sheetService.getSongSheetTypes(userId,page);
        return songSheetTypes;
    }

    @GetMapping("showSongSheetInfo/{songSheetId}")
    @ResponseBody
    public Map<String, Object> showSongSheetInfo(@PathVariable("songSheetId") Integer songSheetId){
        return sheetService.selectById(songSheetId);
    }

    @RequestMapping(value = "/search")
    public String search(@RequestParam(value = "searchName", required = false) String searchName,
                         @RequestParam(value = "searchNickName", required = false) String searchNickName,
                         Map<String, Object> map){
        String msg = "";
        if ("".equals(searchName) && "".equals(searchNickName)) {
            msg = "请至少填一个搜索项";
        }else {
            map.put("songSheets",sheetService.search(searchName,searchNickName));
            map.put("count",sheetService.countSeach(searchName,searchNickName));
        }
        map.put("msg",msg);
        return "picture-list.html";
    }

    @PutMapping("saveSongSheet")
    @ResponseBody
    public boolean saveSongSheet(@RequestBody Songsheet songsheet){
        int i = sheetService.saveSongSheet(songsheet);
        return i > 0 ? true : false;
    }

    @GetMapping("getSongSheetSongs")
    @ResponseBody
    public List getSongSheetSongs(@RequestParam Integer songSheetId,@RequestParam Integer page){

        List songSheetSongsBySongSheetId = sheetService.getSongSheetSongsBySongSheetId(songSheetId,(page-1)*8);
        return songSheetSongsBySongSheetId;
    }

    @DeleteMapping("remove")
    @ResponseBody
    public boolean remove(@RequestBody MusicSongsheet musicSongsheet){
        System.out.println(musicSongsheet);
        int i = sheetService.removeMusicSongSheet(musicSongsheet);
        return i > 0 ? true : false;
    }
}
